<?php

class UchelpController extends BaseController{

    public function init(){
        parent::init();
        if(!$this->member['member_id']){
            if(IS_AJAX){
                $this->ajaxReturn(array('err'=>1,'msg'=>L('_no_login')));
            }else{
                $this->error(L('_no_login'));
            }
        }
    }

    //首页
    public function indexAction(){
        $this->show('Uchelp/index.php');
    }

    public function detailAction(){
        $id = $this->input('get','id',0);
        $MemberFaqModel = new Model('MemberFaq');
        $question = $MemberFaqModel->where(array('id'=>$id,'member_id'=>$this->member['member_id']))->find();
        if($question){
            $question['answer'] = $MemberFaqModel->where(array('pid'=>$question['id']))->order('id asc')->select();
        }
        $this->assign('question',$question);
        $this->show('Uchelp/detail.php');
    }

    public function get_my_faqAction(){
        $pageSize = 6;
        $MemberFaqModel = new Model('MemberFaq');
        $where['pid'] = 0;
        $where['member_id'] = $this->member['member_id'];
        $title = $this->input('get','title');
        if(isset($title) && $title!=''){
            $where['title'] = array('like',"%".$title."%");
        }
        $count      = $MemberFaqModel
                    ->where($where)
                    ->count();// 查询满足要求的总记录数
        $Page       = new Page($count,$pageSize,$this->input('param'));
        $Page->setConfig('theme','<div class="page no-margin pull-right">%UP_PAGE% %FIRST% %LINK_PAGE% %END% %DOWN_PAGE%</div>');
        $Page->setConfig('next','»');
        $Page->setConfig('prev','«');
        $Page->setConfig('header','');
        $show       = $Page->show();// 分页显示输出
        $list = $MemberFaqModel->where($where)
                    ->limit($Page->firstRow.','.$Page->listRows)
                    ->order('add_time desc')
                    ->select();
        foreach ($list as $key => $value) {
            $list[$key]['addtime'] = date('Y-m-d H:i:s',$value['add_time']);
            $list[$key]['detail'] = url('detail',array('id'=>$value['id']));
        }
        $this->ajaxReturn(array('err'=>0,'list'=>$list,'page'=>$show));
    }

    public function addAction(){
        if(IS_POST){
            $MemberFaqModel = new Model('MemberFaq');
            $post = $this->input('post','info');
            $validate = array(
                array('title','require','title>'.L('_title_required')),
                array('text','require','text>'.L('_content_required')),
            );
            $check_result = $MemberFaqModel->validate($validate)->create($post);
            if (!$check_result){
                $json=array('err'=>1);
                list($json['key'],$json['msg'])=explode('>',$MemberFaqModel->getError());
                $this->ajaxReturn($json);
            }else{
                $post['add_time'] = time();
                $post['add_ip'] = get_client_ip();
                $post['member_id'] = $this->member['member_id'];
                $result = $MemberFaqModel->add($post);
                echo $MemberFaqModel->_sql();exit;
                if($result){
                    MemberModel::log($this->member['member_id'],$post,1);
                    $this->ajaxReturn(array('err'=>0,'msg'=>L('_success')));
                }else{
                    MemberModel::log($this->member['member_id'],$post,0);
                    $this->ajaxReturn(array('err'=>1,'msg'=>L('_err')));
                }
            }
        }
    }

}